// pages/train/train.js
var app=getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    viedeList:[],
    on:0,
    imgPath:app.globalData.imgPath,
    commentList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.videoContext = wx.createVideoContext('trainV')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let self = this;
    wx.request({
      url: app.globalData.reqPath + 'shop/LearnVideos',
      header: {
        'authorization': app.globalData.authorization
      },
      data: {
        limit: -1
      },
      success: function ({ data }) {
        if (data.code == 200) {
          wx.createVideoContext('v0', this);
          self.setData({ viedeList: data.data })
          if (data.data.length > 0) {

            self.getrateList()
          }
        }
      }
    }) 

    var videoContextPrev = wx.createVideoContext('v' + this.data.on);
    videoContextPrev.pause()
    //let self=this;
    if (this.data.viedeList.length>0){
      setTimeout(function () { self.getrateList(); }, 600)
    }
    
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var videoContextPrev = wx.createVideoContext('v' + this.data.on);
    videoContextPrev.pause()
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  toplay:function(e){
    let index=e.currentTarget.dataset.index;
    wx.createVideoContext('v'+index, this);
    //var index = video.getDataSet(event, 'index');

    // if (!this.data.on) { // 没有播放时播放视频
    //   this.setData({
    //     on: index
    //   })
    //   var videoContext = wx.createVideoContext('v' + index)
    //   videoContext.play()
    // } else {
    console.log(this.data.on) 
      var videoContextPrev = wx.createVideoContext('v' + this.data.on)
      videoContextPrev.stop()
      this.setData({
        on: index
      })
      var videoContextCurrent = wx.createVideoContext('v' + index)
      
      videoContextCurrent.play()
   // }
    this.changecomment(index);
  },
  changecomment:function(index){//改评论列表
    let objectId=this.data.viedeList[index].id;
    let self=this;
    wx.request({
      url: app.globalData.reqPath + 'shop/Comments',
      header: {
        'authorization': app.globalData.authorization
      },
      data: {
        type: 2,
        objectId: objectId
      },
      success: function ({ data }) {
        if (data.code == 200) {
          self.setData({commentList: data.data })
        }
      }
    })
  },
  getrateList:function(){
    let self=this;
    console.log(this.data.viedeList[this.data.on].id)
    wx.request({
      url: app.globalData.reqPath + 'shop/Comments',
      header: {
        'authorization': app.globalData.authorization
      },
      data: {
        type: 2,
        objectId: this.data.viedeList[this.data.on].id
      },
      success: function ({ data }) {
        if (data.code == 200) {
          let list = data.data;
          self.setData({ commentList: list })
        }
      }
    })
  }
})